import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Base64;
import java.util.Date;
import java.util.UUID;

public class FirstTest {

    public static final String key = "123456";

    public static void main(String[] args) {

        String token = generateJwt("tuyou");
        System.out.println(token);
        String subject = parseJwt(token);
        System.out.println(subject);

        System.out.println(new String(Base64.getDecoder().decode("eyJhbGciOiJIUzI1NiJ9")));
        System.out.println(new String(Base64.getDecoder().decode("eyJzdWIiOiJ0dXlvdSIsImp0aSI6ImVjNWYwNjc4LTRlZDUtNGY5Zi04ODc4LWJiMTc0YWE2NmYzOSIsImlhdCI6MTU4NDg4Nzg3OSwiZXhwIjoxNTg0ODk3ODc5fQ")));
    }


    public static String generateJwt(String subject) {
        Date now = new Date();
        Date expiredTime = new Date(now.getTime() + 10000000);

        JwtBuilder jwtBuilder = Jwts.builder()
                .setSubject(subject)
                .setId(UUID.randomUUID().toString())
                .setIssuedAt(now)
                .setExpiration(expiredTime)
                .signWith(SignatureAlgorithm.HS256, key)
                ;
        return jwtBuilder.compact();
    }

    public static String parseJwt(String jwtToken) {
        Claims body = Jwts.parser()
                .setSigningKey(key)
                .parseClaimsJws(jwtToken)
                .getBody();
        return body.getSubject();
    }
}
